/**
*      author: Y_Yao
*      created: 2025-02-21 20:43:02
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int MAX = 1414213 + 7;
int d;
int ans = INF;

void solve()
{
    cin >> d;
    vector<int> all;
    for(int x = 0; x < MAX;x++)
    {
        all.push_back(x*x);
    }

    for(int i = 0;i < all.size();i++)
    {
        auto it = lower_bound(all.begin(),all.end(),d - all[i]);
        if(it != all.end())
        {
            ans = min(ans,abs(all[i] + *it - d));
        }
        if(it != all.begin())
        {
            it--;
            ans = min(ans,abs(all[i] + *it - d));
        }
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}